package com.dcbeida.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.bean.HistoryOfSmokingBean;
import com.dcbeida.widget.wheel.AbstractWheelTextAdapter;
import com.dcbeida.widget.wheel.OnWheelChangedListener;
import com.dcbeida.widget.wheel.OnWheelClickedListener;
import com.dcbeida.widget.wheel.WheelView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 吸烟史适配器
 * Created by swt on 2016/12/26.
 */
public class HospitalOfSmokingAdapter extends BaseAdapter {

    public int index = -1;
    public String xysYear = "1年";
    public int itemIndex = 0;
    ArrayList<String> HH = new ArrayList<String>();

    private ArrayList<HistoryOfSmokingBean.ListBean> mArrayList = new ArrayList<>();
    private Context mContext;

    public HospitalOfSmokingAdapter(Context context) {
        super();
        this.mContext = context;
        HH.add("1年");
        HH.add("2年");
        HH.add("3年");
        HH.add("4年");
        HH.add("5年");
        HH.add("6年");
        HH.add("7年");
        HH.add("8年");
        HH.add("9年");
        HH.add("10年以上");
    }


    @Override
    public int getCount() {
        if (mArrayList == null) {
            return 0;
        } else {
            return this.mArrayList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mArrayList == null) {
            return null;
        } else {
            return this.mArrayList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.history_of_smoking_item, null, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        HistoryOfSmokingBean.ListBean hospitalBean = mArrayList.get(position);
        holder.tvName.setText(hospitalBean.getName());
        if (position == mArrayList.size() - 1) {
            holder.llLine.setVisibility(View.GONE);
        } else {
            holder.llLine.setVisibility(View.VISIBLE);
        }


        final ViewHolder finalHolder = holder;

        holder.wheelview1.setVisibleItems(3);
        holder.wheelview1.setWheelBackground(R.drawable.wheel_bg_holo_c_f5);
        holder.wheelview1.setWheelForeground(R.drawable.wheel_val_holo);
        holder.wheelview1.setViewAdapter(new MyWheelAdapter(mContext, HH));
        holder.wheelview1.addClickingListener(new OnWheelClickedListener() {
            @Override
            public void onItemClicked(WheelView wheel, int itemIndex) {
                wheel.setCurrentItem(itemIndex);
            }
        });
        holder.wheelview1.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                itemIndex = newValue;
                if (finalHolder.ivXy.getVisibility() == View.VISIBLE) {
                    if (wheel == finalHolder.wheelview1) {
                        xysYear = HH.get(newValue);
                    }
                }
            }
        });

        if (this.index == position) {
            if (hospitalBean.getId() == 4) {
                holder.ivXy.setVisibility(View.VISIBLE);
                holder.wheelview1.setVisibility(View.GONE);
                holder.wheelview1.setCurrentItem(0);
            } else {
                holder.ivXy.setVisibility(View.VISIBLE);
                holder.wheelview1.setVisibility(View.VISIBLE);
                holder.wheelview1.setCurrentItem(itemIndex);
            }
        } else {
            holder.ivXy.setVisibility(View.GONE);
            holder.wheelview1.setVisibility(View.GONE);
            holder.wheelview1.setCurrentItem(0);
        }

        return convertView;
    }


    public ArrayList<HistoryOfSmokingBean.ListBean> getmArrayList() {
        return mArrayList;
    }

    public void setmArrayList(ArrayList<HistoryOfSmokingBean.ListBean> mArrayList) {
        this.mArrayList.clear();
        if (null != mArrayList && mArrayList.size() >= 0) {
            this.mArrayList.addAll(mArrayList);
        }
        notifyDataSetChanged();
    }

    public void setonItemClick(int position, View view) {

        this.index = position;

        notifyDataSetChanged();
    }


    class ViewHolder {
        @Bind(R.id.iv_xy)
        ImageView ivXy;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.ll_xy)
        LinearLayout llXy;
        @Bind(R.id.wheelview1)
        WheelView wheelview1;
        @Bind(R.id.ll_line)
        LinearLayout llLine;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class MyWheelAdapter extends AbstractWheelTextAdapter {

        private List<String> data;

        protected MyWheelAdapter(Context context, List<String> data) {
            super(context, R.layout.wheel_item, NO_RESOURCE);
            setItemTextResource(R.id.tvItem);
            this.data = data;
        }

        @Override
        public int getItemsCount() {
            return data.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            return data.get(index);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            return view;
        }
    }

}
